package com.hw.controller;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author F1335240
 * @version 1.0
 * @date 2020/5/11 10:04
 **/
@Controller
public class MainController {

    @GetMapping("/main")
    public ModelAndView toMainPage() {
        //获取登录的用户名
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = null;
        if (principal instanceof UserDetails) {
            username = ((UserDetails) principal).getUsername();
        } else {
            username = principal.toString();
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("main");
        modelAndView.addObject("username", username);
        return modelAndView;
    }

    @GetMapping("/home")
    public String toHome() {
        return "home";
    }

    @GetMapping("/error")
    public String toErrorPage() {
        return "error/error";
    }
}
